/**
 * ChunkReader.java
 * Copyright (C) 2013 Konstantin Grigoryev
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package aspid.artwork;

import java.io.*;
import aspid.artwork.chunk.*;

import javax.xml.crypto.Data;


public class ChunkReader
    implements ChunkVisitor
{
    DataInput input;

    public ChunkReader( DataInput input )
    {
        this.input = input;
    }

    public void readChunk( Chunk chunk )
        throws IOException
    {
        try {
            chunk.acceptVisitor(this);
        }
        catch (IOException ex) {
            throw ex;
        }
        catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public void visit( ListChunk chunk )
        throws IOException
    {
        chunk.headerLength = input.readInt();
        chunk.numItems     = input.readInt();
        chunk.extraBytes   = new byte[chunk.headerLength - 12];
        input.readFully(chunk.extraBytes);
    }

    public void visit( ItemChunk chunk )
        throws IOException
    {
        chunk.headerLength = input.readInt();
        chunk.totalLength  = input.readInt();
        chunk.extraBytes   = new byte[chunk.headerLength - 12];
        input.readFully(chunk.extraBytes);
    }
}
